package filter

import (
	"adam2/internal/model"
	"adam2/internal/properties"
	"anubis-framework/pkg/io"
	"gorm.io/gorm"
)

// 过滤器：判断用哪根均线作为牛熊线，牛熊线以下的股票都删除
type BullShortLineFilter struct {
	db *gorm.DB
}

// 初始化
func (b *BullShortLineFilter) Init(db *gorm.DB) {
	b.db = db
}

// 执行
func (b *BullShortLineFilter) DoFilter(transactionDate string) {
	io.Infoln("过滤器：判断用哪根均线作为牛熊线，牛熊线以下的股票都删除，交易日期[%s]", transactionDate)

	var stockTransactionDataAllArray model.StockTransactionDataAllArray
	b.db.Raw("select * from stock_transaction_data_all t "+
		"join quant_stock_filter q on t.code_=q.stock_code "+
		"where t.date_=to_date(?,'yyyy-mm-dd')", transactionDate).Scan(&stockTransactionDataAllArray)

	for _, stockTransactionDataAll := range stockTransactionDataAllArray {
		// 表示牛熊线
		var bullShortLine float64

		if stockTransactionDataAll.Bias250 != 0 && stockTransactionDataAll.Bias250 <= properties.QuantProperties_.BiasThresholdTop && stockTransactionDataAll.Bias250 >= properties.QuantProperties_.BiasThresholdBottom {
			bullShortLine = stockTransactionDataAll.Ma250
			if stockTransactionDataAll.ClosePrice < bullShortLine {
				b.db.Exec("delete from quant_stock_filter t where t.stock_code = ?", stockTransactionDataAll.Code)
			}
			continue
		}
		if stockTransactionDataAll.Bias120 != 0 && stockTransactionDataAll.Bias120 <= properties.QuantProperties_.BiasThresholdTop && stockTransactionDataAll.Bias120 >= properties.QuantProperties_.BiasThresholdBottom {
			bullShortLine = stockTransactionDataAll.Ma120
			if stockTransactionDataAll.ClosePrice < bullShortLine {
				b.db.Exec("delete from quant_stock_filter t where t.stock_code = ?", stockTransactionDataAll.Code)
			}
			continue
		}
		if stockTransactionDataAll.Bias60 != 0 && stockTransactionDataAll.Bias60 <= properties.QuantProperties_.BiasThresholdTop && stockTransactionDataAll.Bias60 >= properties.QuantProperties_.BiasThresholdBottom {
			bullShortLine = stockTransactionDataAll.Ma60
			if stockTransactionDataAll.ClosePrice < bullShortLine {
				b.db.Exec("delete from quant_stock_filter t where t.stock_code = ?", stockTransactionDataAll.Code)
			}
			continue
		}
		if stockTransactionDataAll.Bias20 != 0 && stockTransactionDataAll.Bias20 <= properties.QuantProperties_.BiasThresholdTop && stockTransactionDataAll.Bias20 >= properties.QuantProperties_.BiasThresholdBottom {
			bullShortLine = stockTransactionDataAll.Ma20
			if stockTransactionDataAll.ClosePrice < bullShortLine {
				b.db.Exec("delete from quant_stock_filter t where t.stock_code = ?", stockTransactionDataAll.Code)
			}
			continue
		}
		if stockTransactionDataAll.Bias10 != 0 && stockTransactionDataAll.Bias10 <= properties.QuantProperties_.BiasThresholdTop && stockTransactionDataAll.Bias10 >= properties.QuantProperties_.BiasThresholdBottom {
			bullShortLine = stockTransactionDataAll.Ma10
			if stockTransactionDataAll.ClosePrice < bullShortLine {
				b.db.Exec("delete from quant_stock_filter t where t.stock_code = ?", stockTransactionDataAll.Code)
			}
			continue
		}
		if stockTransactionDataAll.Bias5 != 0 && stockTransactionDataAll.Bias5 <= properties.QuantProperties_.BiasThresholdTop && stockTransactionDataAll.Bias5 >= properties.QuantProperties_.BiasThresholdBottom {
			bullShortLine = stockTransactionDataAll.Ma5
			if stockTransactionDataAll.ClosePrice < bullShortLine {
				b.db.Exec("delete from quant_stock_filter t where t.stock_code = ?", stockTransactionDataAll.Code)
			}
			continue
		}
		// 如果最后任何bias指标都无法确定哪一根均线可以作为牛熊线的话，则将前一个交易日的收盘价作为牛熊线。注意：现在数据库中last_close_price字段没有空值
		bullShortLine = stockTransactionDataAll.LastClosePrice
		if stockTransactionDataAll.ClosePrice < bullShortLine {
			b.db.Exec("delete from quant_stock_filter t where t.stock_code = ?", stockTransactionDataAll.Code)
			continue
		}
	}
}
